﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace XmlStudio.Utilities {
    /// <summary>
    /// Helper to check XML names.
    /// </summary>
    public static class XmlChecker {
        /// <summary>
        /// Checks the name of the element.
        /// </summary>
        /// <param name="elementName">Name of the element.</param>
        /// <returns>Errors.</returns>
        public static string CheckElementName(string elementName) {
            try {
                XElement e = new XElement(elementName);
                return string.Empty;
            } catch(XmlException ex) {
                return ex.Message;
            }
        }

        /// <summary>
        /// Checks the element prefix.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <returns>Errors.</returns>
        public static string CheckElementPrefix(string prefix) {
            try {
                //XElement e = new XElement(string.Format("{0}:element", prefix));
                XmlDocument d = new XmlDocument();
                var element = d.CreateElement(prefix, "element", "http://www.xmlstudio.cz/");

                return string.Empty;
            } catch(XmlException ex) {
                return ex.Message;
            }
        }

        /// <summary>
        /// Checks the element namespace URI.
        /// </summary>
        /// <param name="namespaceUri">The namespace URI.</param>
        /// <returns>Errors.</returns>
        public static string CheckElementNamespaceUri(string namespaceUri) {
            try {
                if(!string.IsNullOrEmpty(namespaceUri)) {
                    XElement e = new XElement(XName.Get(string.Format("{{{0}}}element", namespaceUri)));
                }

                return string.Empty;
            } catch(XmlException ex) {
                return ex.Message;
            }
        }

        /// <summary>
        /// Checks the name of the attribute.
        /// </summary>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>Errors.</returns>
        public static string CheckAttributeName(string attributeName) {
            try {
                XAttribute a = new XAttribute(attributeName, "value");
                return string.Empty;
            } catch(XmlException ex) {
                return ex.Message;
            }
        }

        /// <summary>
        /// Checks the attribute value.
        /// </summary>
        /// <param name="attributeValue">The attribute value.</param>
        /// <returns>Errors.</returns>
        public static string CheckAttributeValue(string attributeValue) {
            try {
                //XAttribute a = new XAttribute("attribute", attributeValue);
                XmlDocument d = new XmlDocument();
                var att = d.CreateAttribute("attribute");
                att.Value = attributeValue;

                return string.Empty;
            } catch(XmlException ex) {
                return ex.Message;
            }
        }
    }
}
